package com.example.chartapp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
/**
 * @PACKAGE_NAME: com.example.chartapp
 * @CLASS_NAME: WebSocketConfig
 * @USER: BTS&ARMY
 * @Date 2024/10/9 11:16
 * @Version 1.0
 */

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    // 使用 @Autowired 注解注入 Spring 管理的 ChatWebSocketHandler
    @Autowired
    private ChatWebSocketHandler chatWebSocketHandler;  // 自动注入


//    private final ChatWebSocketHandler chatWebSocketHandler;
//
//    @Autowired
//    public WebSocketConfig(ChatWebSocketHandler chatWebSocketHandler) {
//        this.chatWebSocketHandler = chatWebSocketHandler;
//    }
//    @Autowired
//    private ChatWebSocketHandler chatWebSocketHandler;  // 自动注入 ChatWebSocketHandler
//
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(chatWebSocketHandler, "/chat").setAllowedOrigins("*");
    }


//    @Override
//    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
//        registry.addHandler(new ChatWebSocketHandler(), "/chat").setAllowedOrigins("*");
//    }
}
